package cn.com.ddapp.ddm.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.NearstoreAdpter;
import cn.com.ddapp.ddm.adapter.ShareItemImageAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.bean.CircleBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TextUtil;
import cn.com.ddapp.ddm.widget.CircleImageView;


/**
 * Created by Administrator on 2016/7/29 0029.
 */
public class CircleManageActivity extends BaseActivity {
	@BindView(R.id.empty)
	EmptyLayout emptyLayout;
	@BindView(R.id.img_error_layout)
	ImageView img_error_layout;
	@BindView(R.id.manager_img)
	CircleImageView manager_img;
    @BindView(R.id.manager_name)
	TextView manager_name;
	@BindView(R.id.manager_circles)
	TextView manager_circles;
	@BindView(R.id.manager_gridview)
	GridView manager_gridview;
	@BindView(R.id.mine_drafit)
	RelativeLayout mine_drafit;
	/*@BindView(R.id.noshare)
	TextView noshare;*/
	@BindView(R.id.more_circle)
	RelativeLayout more_circle;
	private NearstoreAdpter nAdapter;
	private List<CircleBean.CircleList> nList;
	private int st;
	private String userId,headImg,userName,imgs;
	private Context context;
	private ShareItemImageAdapter imgAdapter;
	Bitmap mineBitmap;

	public static void startIntent(Context context){
		Intent intent=new Intent(context,CircleManageActivity.class);
		context.startActivity(intent);
	}

	@Override
	public void initView() {

		setTitle("分享管理");
		setTitleLeftImg(R.mipmap.back_icon);
		context=getApplicationContext();
		userId = SPUtils.getStringData(context, SPUtils.USERID);
		headImg = getIntent().getStringExtra("sheadImg");
		userName = getIntent().getStringExtra("nickName");
		mineBitmap = getIntent().getParcelableExtra("bitmap");

		/*Glide.with(context).load(OKUtilAPI.imgBaseUrl+headImg)
				.placeholder(R.mipmap.src)
				.into(manager_img);*/
		TextUtil.setFlags(manager_circles);
        try {
            manager_name.setText(URLDecoder.decode(userName, "utf-8"));
        }catch (Exception e){
            e.printStackTrace();
        }

		if (mineBitmap == null){
			if(!TextUtils.isEmpty(headImg)){
                if(headImg.contains("http")){
                    Glide.with(context).load(headImg).into(manager_img);
                }else{
                    Glide.with(context).load(OKUtilAPI.imgBaseUrl+headImg).into(manager_img);
                }
			}
		}
		else {
			manager_img.setImageBitmap(mineBitmap);
		}
		requestData();
	}



	private void requestData() {
		emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
		final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
			@Override
			public void onMyResponse(String response, int id) {
				CircleBean bean=gson.fromJson(response,CircleBean.class);
				if(!bean.getResult().equals("OK")){
					emptyLayout.setErrorType(EmptyLayout.NODATA);
				}else if(bean.getResult().equals("OK") && bean.getCircleList().size()==0){
					//noshare.setVisibility(View.VISIBLE);
					manager_gridview.setVisibility(View.GONE);
					manager_circles.setText("0");
					//more_circle.setVisibility(View.GONE);
				}else{
					//noshare.setVisibility(View.GONE);
					manager_gridview.setVisibility(View.VISIBLE);
					more_circle.setVisibility(View.VISIBLE);
					nList=bean.getCircleList();
					manager_circles.setText(String.valueOf(nList.size()));
					String imgs=nList.get(0).getImgs();
                    Log.i("aaa","---"+imgs);
//					List<MyShareBean.MyCircle1.CiecleData> dataList=nList.getData();
//					imgs=dataList.get(0).getImgs();
					String img[]=imgs.split(",");
                    if(imgs.equals("")){
                        manager_gridview.setVisibility(View.GONE);
                    }else{
                        manager_gridview.setVisibility(View.VISIBLE);
                        List<String> imgList=new ArrayList<>();
                        for(int p=0;p<img.length;p++){
                            imgList.add(img[p]);
                        }
                        Log.i("aaa","-imgList--"+imgList.size());
                        imgAdapter=new ShareItemImageAdapter(context,imgList);
                        manager_gridview.setAdapter(imgAdapter);
                    }
				}
			}

		};
		String params="userId=" +userId+"&status="+1;
		//获取发现列表
		OKUtilAPI.post(OKUtilAPI.circlelist, params, okUtilCallback);
	}
	@Override
	public void initData() {
//		nAdapter=new NearstoreAdpter(this,nList);
//		nearshop_list.setAdapter(nAdapter);
	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_foundmanager;
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}



	@OnClick({R.id.img_error_layout,R.id.more_circle,R.id.mine_drafit})
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
			case R.id.img_error_layout:
				requestData();
				break;

			case R.id.more_circle:
				intent=new Intent(context,MyDraftActivity.class);
				intent.putExtra("type","1");
				intent.putExtra("userId",userId);
				startActivity(intent);
				break;
			case R.id.mine_drafit:
				intent=new Intent(context,MyDraftActivity.class);
				intent.putExtra("type","0");
				intent.putExtra("userId",userId);
				startActivity(intent);
				break;
		}
	}

}
